package org.liveboardgames.agricola.control.delegates
{
	import com.adobe.cairngorm.business.ServiceLocator;

	import mx.collections.ListCollectionView;
	import mx.rpc.IResponder;

	import org.liveboardgames.agricola.control.delegates.helper.GameHelper;
	import org.liveboardgames.agricola.control.events.DropPeepEvent;
	import org.liveboardgames.agricola.model.AgricolaModelLocator;
	import org.liveboardgames.agricola.vo.card.Card;
	import org.liveboardgames.agricola.vo.common.Peep;

	public class DropPeepDelegate
	{
		private var _responder:IResponder;

		private var _service:Object;

		public function DropPeepDelegate(responder:IResponder)
		{
			this._service = ServiceLocator.getInstance().getRemoteObject("peepService");
			this._responder = responder;
		}

		public function dropPeep(dropPeepEvent:DropPeepEvent):void
		{
			var peep:Peep = dropPeepEvent.peep;
			var card:Card = dropPeepEvent.card;

			var peepCall:Object = _service.dropPeep(AgricolaModelLocator.getInstance().game.id, peep.id, card.cardId);
			peepCall.resultHandler = _responder.result;
			peepCall.faultHandler = _responder.fault;
		}

		private function performDrop(peep:Peep, card:Card):void
		{
			trace("Performing drop on card: " + card.cardName);
			AgricolaModelLocator.getInstance().game.hasPlayerPlayedPeep = true;
			if (peep != null)
			{
				AgricolaModelLocator.getInstance().actionController.player = GameHelper.getPeepOwner(AgricolaModelLocator.
					getInstance().game, peep);
			}

			var peepTriggeredActions:ListCollectionView = GameHelper.getPeepTriggeredActions(card);

			AgricolaModelLocator.getInstance().actionController.setCard(card, peepTriggeredActions);
			AgricolaModelLocator.getInstance().actionController.executeAllActions();
		}
	}
}